<?php

namespace app\models;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "gky_msg_user".
 *
 * @property string $id
 * @property string $msgid
 * @property string $userid
 * @property integer $isread
 * @property string $readtime
 * @property integer $iscollect
 */
class GkyMsgUser extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'gky_msg_user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['msgid', 'isread', 'iscollect'], 'integer'],
            [['userid'], 'string', 'max' => 64],
        ];
    }

    public function getMsg(){
        return $this->hasOne(Msg::className(),['id'=>'msgid']);
    }
    
    
    public function getUser(){
        return $this->hasOne(GkyQyhUser::className(),['id'=>'userid']);
    }

    public function getQyhUser(){
        return $this->hasOne(GkyQyhUser::className(),['id'=>'userid']);
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'msgid' => 'Msgid',
            'userid' => 'Userid',
            'isread' => 'Isread',
            'readtime' => 'Readtime',
            'iscollect' => 'Iscollect',
        ];
    }

    /**
     * 获取接收的消息列表
     *
     * @param $userid
     * @param $pageNow
     * @param $pageSize
     * @return array
     */
    public static function findAcceptList($userid, $pageNow, $pageSize){

        $list = GkyMsgUser::find()
            ->where(['userid'=>$userid])
            ->orderBy(['addtime' => SORT_DESC])
            ->offset(($pageNow - 1) * $pageSize)
            ->limit($pageSize)
            ->all();
        $results = [];
        foreach($list as $model){
            $result = [];
            $result['username'] = $model->msg->qyhUser->name;
            $result['avatar'] = $model->msg->qyhUser->avatar;
            $result['accepters'] = GkyMsgUser::findAccepter($model->msgid);
            $result = array_merge($result,ArrayHelper::toArray($model->msg));
            $results[] = $result;
        }
        return $results;
    }

    /**
     * 获取发送的消息列表
     *
     * @param $userid
     * @param $pageNow
     * @param $pageSize
     * @return array
     */
    public static function findSendList($userid, $pageNow, $pageSize){

        $list = Msg::find()
            ->where(['addmanid'=>$userid])
            ->orderBy(['addtime' => SORT_DESC])
            ->offset(($pageNow - 1) * $pageSize)
            ->limit($pageSize)
            ->all();

        $results = [];
        foreach($list as $model){
            $result = [];
            $result = array_merge($result,ArrayHelper::toArray($model));
            $result['username'] = $model->qyhUser->name;
            $result['avatar'] = $model->qyhUser->avatar;
            $result['accepters'] = GkyMsgUser::findAccepter($model->id);
            $results[] = $result;
        }

        return $results;
    }

    public static function searchBySend($userid,$str){
        $list = Msg::find()
            ->where(['and','addmanid'=>$userid,['or',['like','title',$str],['like','description',$str],['like','content',$str],['like','msgtype',$str]]])
            ->all();

        $results = [];
        foreach($list as $model){
            $result = [];
            $result = array_merge($result,ArrayHelper::toArray($model));
            $result['username'] = $model->qyhUser->name;
            $result['avatar'] = $model->qyhUser->avatar;
            $result['accepters'] = GkyMsgUser::findAccepter($model->id);
            $results[] = $result;
        }

        return $results;
    }

    /**
     * 查找未读消息
     */
    public static function findUnreadList($userid, $pageNow, $pageSize){

        $list = GkyMsgUser::find()
            ->where(['userid'=>$userid])
            ->andwhere(['or', 'isread=0', 'isread is null'])
            ->orderBy(['addtime' => SORT_DESC])
            ->offset(($pageNow - 1) * $pageSize)
            ->limit($pageSize)
            ->all();
        $count = GkyMsgUser::find()
            ->where(['userid'=>$userid])
            ->andwhere(['or', 'isread=0', 'isread is null'])
            ->count();
        $results = [];
        $results['count'] = $count;
        $results['msgs'] = [];
        foreach($list as $model){
            $result = ArrayHelper::toArray($model->msg);
            $result['username'] = $model->msg->qyhUser->name;
            $result['avatar'] = $model->msg->qyhUser->avatar;
            $result['accepters'] = GkyMsgUser::findAccepter($model->msgid);
            $results['msgs'][] = $result;
        }
        return $results;
    }

    public static function findCollectList($userid, $pageNow, $pageSize){

        $list = GkyMsgUser::find()
            ->where(['userid'=>$userid,'iscollect'=>1])
            ->orderBy(['addtime' => SORT_DESC])
            ->offset(($pageNow - 1) * $pageSize)
            ->limit($pageSize)
            ->all();
        $results = [];
        foreach($list as $model){
            $result = ArrayHelper::toArray($model->msg);
            $result['username'] = $model->msg->qyhUser->name;
            $result['avatar'] = $model->msg->qyhUser->avatar;
            $result['accepters'] = GkyMsgUser::findAccepter($model->msgid);
            $results[] = $result;
        }
        return $results;
    }

    public static function findByMsgidAndUserid($msgid, $userid){

        $msgUser = GkyMsgUser::find()
            ->where(['msgid'=>$msgid,'userid'=>$userid])
            ->one();

        return $msgUser;
    }

    /**
     * 根据msgid查询接收者姓名,和是否已读
     *
     * @param $msgid msg id
     */
    public static function findAccepter($msgid){
        $msgUsers = GkyMsgUser::find()->where(['msgid'=>$msgid])->all();
        $result = [];
        foreach($msgUsers as $msgUser){
            $accepter['accepter'] = $msgUser->qyhUser->name;
            $accepter['isread'] = $msgUser->isread;
            $result[] = $accepter;
        }

        return $result;
    }
}
